package com.meilai.project.controller.web.system;

import com.meilai.project.dto.CommonResult;
import com.meilai.project.entity.system.Dictionary;
import com.meilai.project.service.system.DictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author yxg
 * @Date 2020/8/25 10:17
 */
@Api(value = "Web端字典控制器", description = "Web端字典相关")
@RestController
@RequestMapping("api/web/dict")
public class WebDictionaryController {
    @Autowired
    private DictionaryService service;

    @GetMapping("point_type_select_options_list")
    @ApiOperation(value = "点位类型级联选择格式")
    public CommonResult pointTypeSelectOptions() {
        return new CommonResult().success(service.pointTypeSelectOptions());
    }

    @GetMapping("index_list")
    @ApiOperation(value = "获取字典列表(一级、二级)")
    public CommonResult list1stAnd2nd(@RequestParam(value = "name", required = false) String name,
                                      @RequestParam(value = "status", required = false) Integer status) {
        return new CommonResult().success(service.list1stAnd2nd(name, status));
    }

    @GetMapping("index_list3rd")
    @ApiOperation(value = "获取字典列表(三级)")
    public CommonResult list3rd(@RequestParam(value = "pid", required = false) Long pid,
                                @RequestParam(value = "name", required = false) String name,
                                @RequestParam(value = "status", required = false) Integer status,
                                @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        return new CommonResult().success(service.list3rdAll(pid, name, status, pageNum, pageSize));
    }

    @GetMapping("index_list4th")
    @ApiOperation(value = "获取字典列表(四级)")
    public CommonResult list4th(@RequestParam(value = "pid", required = false) Long pid,
                                @RequestParam(value = "name", required = false) String name,
                                @RequestParam(value = "status", required = false) Integer status,
                                @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        return new CommonResult().success(service.list4thAll(pid, name, status, pageNum, pageSize));
    }


    @GetMapping("list_all3rd_bycode")
    @ApiOperation(value = "通过code获取下级字典列表")
    public CommonResult listAll3rdBycode(@RequestParam(value = "code", required = true) String code) {
        return new CommonResult().success(service.listByCode(code));
    }

    @GetMapping("list_all4th_by2ndcode")
    @ApiOperation(value = "通过code获取四级字典列表")
    public CommonResult listAll4thBycode(@RequestParam(value = "code", required = true) String code) {
        return new CommonResult().success(service.list4thBy2ndCode(code));
    }

    @GetMapping("list_all3rd_bycode_contrain_del_close")
    @ApiOperation(value = "通过code获取字典列表(三级)，包括删除的和禁用的")
    public CommonResult listAll3rdBycodeContrainDelClose(@RequestParam(value = "code", required = true) String code) {
        return new CommonResult().success(service.listByPCodeAll(code));
    }

    @GetMapping("list_all3rd_bycode_contrain_del_close_door")
    @ApiOperation(value = "通过code获取字典列表(三级)，包括删除的和禁用的，查询主库主库")
    public CommonResult listAll3rdBycodeContrainDelCloseDoor(@RequestParam(value = "code", required = true) String code) {
        return new CommonResult().success(service.listByPCodeAllDoor(code));
    }

    @GetMapping("list_all3rd_bytopcode")
    @ApiOperation(value = "通过顶级code获取字典列表(三级)")
    public CommonResult listAll3rdByTopcode(@RequestParam(value = "code", required = true) String code) {
        return new CommonResult().success(service.listByTopCode(code));
    }

    @GetMapping("list_all3rd_bytopcode_contrain_del_close")
    @ApiOperation(value = "通过顶级code获取字典列表(三级)，包括删除的和禁用的")
    public CommonResult listAll3rdByTopcodeContrainDelClose(@RequestParam(value = "code", required = true) String code) {
        return new CommonResult().success(service.listByTopCodeAll(code));
    }

    @GetMapping("list_all3rd_bytopcode_contrain_del_close_door")
    @ApiOperation(value = "通过顶级code获取字典列表(三级)，包括删除的和禁用的，查询主库字典")
    public CommonResult listAll3rdByTopcodeContrainDelCloseDoor(@RequestParam(value = "code", required = true) String code) {
        return new CommonResult().success(service.listByTopCodeAllDoor(code));
    }

    @PostMapping("add")
    @ApiOperation(value = "新增字典")
    public CommonResult add1st(@RequestBody Dictionary createParams) {
        try {
            int row = service.add(createParams);
            if (row == 1) {
                return new CommonResult().success("新增成功");
            } else {
                return new CommonResult().failed("新增失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @PostMapping("edit")
    @ApiOperation(value = "修改字典")
    public CommonResult edit(@RequestBody Dictionary createParams) {
        try {
            int row = service.edit(createParams);
            if (row == 1) {
                return new CommonResult().success("修改成功");
            } else {
                return new CommonResult().failed("修改失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @PutMapping("del")
    @ApiOperation(value = "删除字典")
    public CommonResult del(@RequestParam(value = "ids[]") List<Integer> ids) {
        if (service.del(ids)) {
            return new CommonResult().success();
        }
        return new CommonResult().failed("操作失败");
    }

    @PutMapping("change_status")
    @ApiOperation(value = "更改字典状态")
    public CommonResult changeStatus(@RequestParam(value = "ids[]") Integer ids, Integer status) {
        try {
            service.changeStatus(ids, status);
            return new CommonResult().success();
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @GetMapping("getTreeDictionaryVODataByCode2level")
    @ApiOperation(value = "根据二级Code 返回树形结构数据 仅有三级和四级数据的树形结构")
    public CommonResult getTreeDictionaryVODataByCode2level(@RequestParam(value = "code") String code) {
        return new CommonResult().success(service.getTreeDictionaryVODataByCode2level(code));
    }
}
